//
//  WKFTime.m
//  质检
//
//  Created by Mac on 2018/3/28.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "WKFTime.h"

@implementation WKFTime
//根据时间和 时间格式获取 时间
+ (NSString*)dateToStringWithFormatter:(NSDateFormatter*)formatter date:(NSDate*)date;
{
    NSString *dateString=[formatter stringFromDate:date];
    return dateString;
}


/**
 根据时间和 时间格式获取 时间

 @param formatterStr <#formatterStr description#>
 @param date <#date description#>
 @return <#return value description#>
 */
+ (NSString*)dateToStringWithFormatterStr:(NSString *)formatterStr date:(NSDate*)date
{
    NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
    [resultFormatter setDateFormat:formatterStr];
    return [WKFTime dateToStringWithFormatter:resultFormatter date:date];
}


/**
 获取当前时间戳

 @return <#return value description#>
 */
+ (NSString *)dateIimeStapStr
{
    NSTimeInterval time = [[NSDate date]  timeIntervalSince1970];
    long long int timesc= (long long int)time;
    NSString *timestamp = [NSString stringWithFormat:@"%lld000",timesc];
    return timestamp;
}


/**
 获取当前时间
 */
+(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    NSLog(@"currentTimeString =  %@",currentTimeString);
    return currentTimeString;
    
}


/**
 获取当前时间戳有两种方法(以秒为单位)
 */
+(NSString *)getNowTimeTimestamp{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    
    return timeSp;
    
}

+(NSString *)getNowTimeTimestamp2{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    return timeString;
}


/**
 //获取当前时间戳 （以毫秒为单位)
 */
+(NSString *)getNowTimeTimestamp3{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
    
    return timeSp;
}
// 根据时间戳 获取时间
+ (NSDate *)dateByTimestamp:(NSString *)timestamp
{
    if (timestamp.length>=10) {
        NSString *tepTime = [timestamp substringWithRange:NSMakeRange(0, 10)];
        return [NSDate dateWithTimeIntervalSince1970:[tepTime intValue]];
    }
    else
    {
        /** test code beigin  */
        return [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
        /** test code end  //kn - 2015年1月24日 */
    }
}
// 根据时间戳 获取时间
+ (NSDate *)dateWithTimeIntervalInMilliSecondSince1970:(double)timeIntervalInMilliSecond
{
    NSDate *ret = nil;
    double timeInterval = timeIntervalInMilliSecond;
    // judge if the argument is in secconds(for former data structure).
    if(timeIntervalInMilliSecond > 140000000000) {
        timeInterval = timeIntervalInMilliSecond / 1000;
    }
    ret = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    return ret;
}


//根据时间戳 和时间格式 获取时间
+ (NSString *)dateToStringWithFormatterStr:(NSString *)formatterStr timestamp:(NSString *)timestamp
{
    if (!timestamp || timestamp.length == 0)
    {
        return @"";
    }
    NSDate *date = [WKFTime dateByTimestamp:timestamp];
    return [WKFTime dateToStringWithFormatterStr:formatterStr date:date];
}
/* ------------------- 输出时间差    -------------- */
+ (NSString *)changDateWithTime:(NSString *)timestamp
{
    if (timestamp.length>=10) {
        NSString *tepTime = [timestamp substringWithRange:NSMakeRange(0, 10)];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[tepTime intValue]];
        
        //获取当前时间     ///// 2013-11-30 05:57:01 +0000
        NSDate *myDate = [NSDate date];
        //DLog(@"myDate = %@",myDate);
        
        //获取时间间隔
        NSTimeInterval secondsBetweenDates= [myDate timeIntervalSinceDate:date];
        secondsBetweenDates /= 60;
        //DLog(@"-------  %lf",secondsBetweenDates);
        
        if (secondsBetweenDates<30) {
            return [NSString stringWithFormat:@"%d分钟前",(int)secondsBetweenDates];
        }
        else if (secondsBetweenDates>=30&&secondsBetweenDates<=40)
        {
            return @"半小时前" ;
        }
        else if (secondsBetweenDates>40&&secondsBetweenDates<60 )
        {
            return [NSString stringWithFormat:@"%d分钟前",(int)secondsBetweenDates];
        }
        else if (secondsBetweenDates>=60&&secondsBetweenDates<60*24)
        {
            return [NSString stringWithFormat:@"%d小时前",(int)(secondsBetweenDates/60)];
        }
        else if (secondsBetweenDates>=60*24&&secondsBetweenDates<60*24*10)
        {
            return [NSString stringWithFormat:@"%d天前",(int)(secondsBetweenDates/60/24)];
        }
        else{
            // 目的格式
            NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
            //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
            [resultFormatter setDateFormat:@"MM月dd日"];
            
            
            //输出格式
            //DLog(@" %@",[DateHelper dateToString:date withFormatter:resultFormatter]);
            return [WKFTime dateToStringWithFormatter:resultFormatter date:date];
        }
    }
    else
        return @"";
}

/* ------------------- 输出时间    -------------- */
+ (NSString *)changDateWithTimeStamp:(NSString *)timestamp
{
    if (timestamp.length>=10)
    {
        NSString *tepTime = [timestamp substringWithRange:NSMakeRange(0, 10)];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[tepTime intValue]];
        
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        [resultFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        return [WKFTime dateToStringWithFormatter:resultFormatter date:date];
    }
    else
        return @"";
}

//主页时间转换
+ (NSString *)changDateWithString:(NSString *)aString{
    
    //解析微博时间 /////Fri Nov 29 19:45:49 +0800 2013
    // NSString *tepStr;
    //aString =@"Fri Nov 29 19:45:49 +0800 2013";
    
    //初始化一个日期格式
    NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];
    iosDateFormater.dateFormat=@"yyyy-MM-dd HH:mm:ss.S";
    //强制设置为美国时间 必须设置，否则无法解析
    iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    //转换微博时间为日期格式
    NSDate *date=[iosDateFormater dateFromString:aString];
    
    //获取当前时间     ///// 2013-11-30 05:57:01 +0000
    NSDate *myDate = [NSDate date];
    //DLog(@"myDate = %@",myDate);
    
    //获取时间间隔
    NSTimeInterval secondsBetweenDates= [myDate timeIntervalSinceDate:date];
    secondsBetweenDates /= 60;
    //DLog(@"-------  %lf",secondsBetweenDates);
    
    if (secondsBetweenDates<30) {
        return [NSString stringWithFormat:@"%d分钟前",(int)secondsBetweenDates];
    }
    else if (secondsBetweenDates>=30&&secondsBetweenDates<=40)
    {
        return @"半小时前" ;
    }
    else if (secondsBetweenDates>40&&secondsBetweenDates<60 )
    {
        return [NSString stringWithFormat:@"%d分钟前",(int)secondsBetweenDates];
    }
    else if (secondsBetweenDates>=60&&secondsBetweenDates<60*24)
    {
        return [NSString stringWithFormat:@"%d小时前",(int)(secondsBetweenDates/60)];
    }
    else if (secondsBetweenDates>=60*24&&secondsBetweenDates<60*24*10)
    {
        return [NSString stringWithFormat:@"%d天前",(int)(secondsBetweenDates/60/24)];
    }
    else{
        // 目的格式
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        [resultFormatter setDateFormat:@"MM月dd日"];
        
        
        //输出格式
        //DLog(@" %@",[DateHelper dateToString:date withFormatter:resultFormatter]);
        return [WKFTime dateToStringWithFormatter:resultFormatter date:date];
    }
    
}


//消息状态列表 时间
+ (NSString *)detailChangeDateWithDate:(NSDate *)aDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd 00:00:00.S"];
    dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
    NSString *currentDateStr1 = [dateFormatter stringFromDate:[NSDate date]];
    
    //获取今天凌晨时间
    NSDate *morningDate = [dateFormatter dateFromString:currentDateStr1];
    //DLog(@"-- 今天凌晨时间 :%@",morningDate);
    
    //获取时间间隔 分钟
    NSTimeInterval secondsBetweenDates= [morningDate timeIntervalSinceDate:aDate];
    secondsBetweenDates /= 60;
    
    if (secondsBetweenDates < 0)
    {
        /* ------------------  一天内  ------------------ */
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        [resultFormatter setDateFormat:@"HH:mm"];
        return [WKFTime dateToStringWithFormatter:resultFormatter date:aDate];
    }
    else if (secondsBetweenDates<60*24)
    {
        /* ------------------  昨天  ------------------ */
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        [resultFormatter setDateFormat:@"昨天 HH:mm"];
        return [WKFTime dateToStringWithFormatter:resultFormatter date:aDate];
    }
    else if (secondsBetweenDates < 60*24*30*12)
    {
        /* ------------------  一年内  ------------------ */
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        [resultFormatter setDateFormat:@"MM-dd"];
        return [WKFTime dateToStringWithFormatter:resultFormatter date:aDate];
    }
    else
    {
        /* ------------------  一年前  ------------------ */
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        //[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        [resultFormatter setDateFormat:@"yyyy-MM-dd"];
        return [WKFTime dateToStringWithFormatter:resultFormatter date:aDate];
    }
}


+ (int)weekOfDevie
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    now=[NSDate date];
    NSDateComponents * comps = [calendar components:unitFlags fromDate:now];
    int  week = (int)[comps weekday];
    // int  month = [comps month];
    // int  day = [comps day];
    //[calendar release];
    return week;
}
+ (int)monthOfDevie
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    now=[NSDate date];
    NSDateComponents *comps = [calendar components:unitFlags fromDate:now];
    //int  week = [comps weekday];
    //int  month = [comps month];
    int  day = (int)[comps day];
    //[calendar release];
    return day;
}



//日期格式转字符串
+ (NSString *)dateToString:(NSDate *)date withDateFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}

//字符串转日期格式
+ (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    return [self worldTimeToChinaTime:date];
}

//将世界时间转化为中国区时间
+ (NSDate *)worldTimeToChinaTime:(NSDate *)date
{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}


@end
